package wps.component.elementsetting {
import flash.events.Event;

import mx.events.FlexEvent;

import spark.components.CheckBox;

import wps.util.EnableChecker;

public class SettingCheckbox extends CheckBox implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _target:Object;

    private var _targetProperty:String;

    private var _needCopy:Boolean = false;

    //----------------------------------
    // booleanMode
    //----------------------------------

    private var _booleanMode:Boolean = true;

    /**
     * 选中或不选中是否只是代表逻辑值，还是另外指定的值<br/>
     * true适用于target的targetProperty属性也是逻辑值<br/>
     * false适用于target的targetProperty属性不是逻辑值，需要指定selectedValue和unselectedValue
     */
    public function get booleanMode():Boolean {
        return _booleanMode;
    }

    public function set booleanMode(booleanMode:Boolean):void {
        _booleanMode = booleanMode;
    }

    //----------------------------------
    // selectedValue
    //----------------------------------

    private var _selectedValue:*;

    /**
     * 当checkbox为选中时，target中属性targetProperty的值<br/>
     * 只在booleanMode为false时有效
     */
    public function get selectedValue():* {
        return _selectedValue;
    }

    public function set selectedValue(selectedValue:*):void {
        _selectedValue = selectedValue;
    }

    //----------------------------------
    // unselectedValue
    //----------------------------------

    private var _unselectedValue:*;

    /**
     * 当checkbox为未选中时，target中属性targetProperty的值<br/>
     * 只在booleanMode为false时有效
     */
    public function get unselectedValue():* {
        return _unselectedValue;
    }

    public function set unselectedValue(unselectedValue:*):void {
        _unselectedValue = unselectedValue;
    }

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function SettingCheckbox() {
        super();

        addEventListener(Event.CHANGE, changeHandler);
        addEventListener(FlexEvent.INITIALIZE, initializeHandler);
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    public function get needCopy():Boolean {
        return _needCopy;
    }

    public function set needCopy(needCopy:Boolean):void {
        _needCopy = needCopy;
    }

    [Bindable]
    public function get target():Object {
        return _target;
    }

    public function set target(target:Object):void {
        _target = target;
        targetChanged();
    }

    public function get targetProperty():String {
        return _targetProperty;
    }

    public function set targetProperty(targetProperty:String):void {
        _targetProperty = targetProperty;
    }

    public function get targetStyle():String {
        return "";
    }

    public function set targetStyle(targetStyle:String):void {

    }

    public function checkEnable():void {
        var checker:EnableChecker = new EnableChecker(this);
        enabled = checker.getEnableValue();
    }

    private function targetChanged():void {
        checkEnable();

        // 是否选中
        if (enabled && target && targetProperty) {
            if (booleanMode) {
                selected = target[targetProperty];
            } else {
                selected = target[targetProperty] == selectedValue;
            }
        } else {
            selected = false;
        }
    }

    //--------------------------------------------------------------------------
    //
    // Event handlers
    //
    //--------------------------------------------------------------------------

    private function changeHandler(e:Event):void {
        if (booleanMode) {
            target[targetProperty] = selected;
        } else {
            target[targetProperty] = selected ? selectedValue : unselectedValue;
        }
    }

    private function initializeHandler(e:FlexEvent):void {
        enabled = target ? true : false;
    }

}
}
